<?php 
/*
**********************************************
JoomLeague RESULTS MODULE
Copyright (c) 2007 JoomLeague.de
**********************************************
This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version.

This header must not be removed. Additional contributions/changes
may be added to this header as long as no information is deleted.
**********************************************

* RESULTS MODULE
*
* 2007-07-31 - Version 0.90
*
* Displays the latest results of given project.
* There are some options for this module, which can be set in the 
* "Parameters" section of the module in Administration.
*

**********************************************
Get the latest version of JoomLeague at:
http://www.joomleague.de//
**********************************************
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

if (file_exists($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php')) {
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/'.$mosConfig_lang.'.php');
} 
else{
  include_once($mosConfig_absolute_path.'/components/com_joomleague/language/english.php');
}

require_once 'administrator/components/com_joomleague/joomleague.class.php';

#comment this line for mambo version < 4.5.1
$params = mosParseParams( $module->params );

$p = $params->project; //default 0
$last_round = $params->last_round; //default 0
$show = $params->show; //default 1
$show_shortnames = $params->show_shortnames; //default 0
$show_link = $params->show_link; //default 1

if($p > 0){
  $project = new JoomLeague( $database );
  $project->load($p);

  if ($last_round == 1)
  $matches = $project->getResults($project->current_round-1);
  else $matches = $project->getResults($project->current_round); 

if($show > 0){?><div align="center"><b><?php echo $project->name;?></b></div><?php }?>
  
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1">
  
<?php
  $favteams = explode(",", $project->fav_team);
  $k = 0;
  foreach($matches as $v){
    $match=new Match($database);
    $match->load($v->match_id);
    $hometeam = new JL_Team($database);
		$hometeam->load($match->matchpart1);
		$guestteam = new JL_Team($database);
		$guestteam->load($match->matchpart2);
    $class = ($k == 0)? 'sectiontableentry1' : 'sectiontableentry2';
    
    if(in_array($hometeam->id, $favteams) && $show_shortnames == 1){
    
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><b><?php echo $hometeam->short_name;?></b></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><?php echo $guestteam->short_name;?></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php
    }
    else if(in_array($guestteam->id, $favteams) && $show_shortnames == 1){
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><?php echo $hometeam->short_name;?></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><b><?php echo $guestteam->short_name;?></b></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php
    }
    else if($show_shortnames == 1){
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><?php echo $hometeam->short_name;?></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><?php echo $guestteam->short_name;?></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php    
    }
    
    if(in_array($hometeam->id, $favteams) && $show_shortnames == 0){
    
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><b><?php echo $hometeam->name;?></b></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><?php echo $guestteam->name;?></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php
    }
    
    else if(in_array($guestteam->id, $favteams) && $show_shortnames == 0){
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><?php echo $hometeam->name;?></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><b><?php echo $guestteam->name;?></b></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php
    }
    else if($show_shortnames == 0){
?>
    <tr class="<?php print $class;?>">
      <td align="right" nowrap="nowrap"><?php echo $hometeam->name;?></td>
      <td align="center">-</td>
      <td align="left" nowrap="nowrap"><?php echo $guestteam->name;?></td>
      <td align="center" nowrap="nowrap"><?php printf("%s:%s",$match->matchpart1_result,$match->matchpart2_result);?></td>
    </tr>
<?php    
    }    
    
    $k = 1 - $k;
  }
?>

</table>
<?php if($show_link == 1){?>
<table width="100%">
      <tr>
        <td align="center">
          <?php 
            if ($last_round == 1)
                $round = $project->current_round-1;
            else $round = $project->current_round; 
            printf('<a href="index.php?option=com_joomleague&amp;func=showResults&amp;p=%s&amp;r=%s&amp;Itemid=%s">'._JL_MODULE_RESULTS_DETAILS.'</a>', $project->id,$round, $Itemid);?>
        </td>
      </tr>
</table>
<?php };?>      

<?php

}
else{
  echo "missing parameter project";
}

?>
